package com.bilibili.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bilibili.dao.VideoCoinMapper;
import com.bilibili.domain.entity.UserCoin;
import com.bilibili.domain.entity.Video;
import com.bilibili.domain.entity.VideoCoin;
import com.bilibili.domain.exception.ConditionException;
import com.bilibili.service.UserCoinService;
import com.bilibili.service.VideoCoinService;
import com.bilibili.service.VideoService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author www
 * @description 针对表【t_video_coin(视频硬币表)】的数据库操作Service实现
 * @createDate 2024-06-17 19:25:25
 */
@Service
public class VideoCoinServiceImpl extends ServiceImpl<VideoCoinMapper, VideoCoin>
        implements VideoCoinService {
    @Resource
    private VideoService videoService;

    @Resource
    private VideoCoinMapper videoCoinMapper;

    @Resource
    private UserCoinService userCoinService;

    LambdaQueryWrapper<VideoCoin> wrapper = new LambdaQueryWrapper<>();

    @Transactional
    @Override
    public void addVideoCoins(VideoCoin videoCoin, Long userId) {
        videoCoin.setUserId(userId);
        Long videoId = videoCoin.getVideoId();
        Integer amount = videoCoin.getAmount();
        if (videoId == null) {
            throw new ConditionException("参数异常！");
        }
        Video video = videoService.getById(videoId);
        if (video == null) {
            throw new ConditionException("非法视频！");
        }

        //查询当前用户是否拥有足够的硬币
        UserCoin userCoin = userCoinService.getUserCoinsAmount(userId);
        Long userCoinAmount = userCoin.getAmount();
        userCoinAmount = userCoinAmount == null ? 0 : userCoinAmount;

        if (amount > userCoinAmount) {
            throw new ConditionException("用户硬币数量不足！");
        }
        //查询当前登录用户对视频已经投了多少硬币
        wrapper.eq(VideoCoin::getUserId, userId)
                .eq(VideoCoin::getVideoId, video);
        VideoCoin dbVideoCoin = videoCoinMapper.selectOne(wrapper);
        //更新投币数量
        if (dbVideoCoin == null) {//还未投币
            videoCoin.setCreateTime(new Date());
            videoCoinMapper.insert(videoCoin);
        } else {
            Integer dbAmount = dbVideoCoin.getAmount();
            dbAmount += amount;
            //更新视频投币
            videoCoin.setAmount(dbAmount);
            videoCoin.setUpdateTime(new Date());
            videoCoinMapper.update(videoCoin, null);
        }

        //更新当前用户硬币总数
        userCoinService.updateCoinsAmount(userId, (userCoinAmount - amount));
    }

    @Override
    public Map<String, Object> getVideoCoins(Long videoId, Long userId) {
        wrapper.eq(VideoCoin::getVideoId, videoId);
        List<VideoCoin> videoCoins = videoCoinMapper.selectList(wrapper);
        Long count = 0l;
        for (VideoCoin videoCoin : videoCoins) {
            count += videoCoin.getAmount();
        }

        wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VideoCoin::getVideoId, videoId)
                .eq(VideoCoin::getUserId, userId);
        VideoCoin videoCoin = videoCoinMapper.selectOne(wrapper);
        boolean coins = videoCoin != null ? true : false;
        Map<String, Object> map = new HashMap<>();
        map.put("count", count);
        map.put("coins", coins);
        return map;
    }
}




